Ext.onReady(function(){
	  Ext.QuickTips.init();
	windowst.show();
	fenye(0);
});
var fielmenu=new Ext.menu.Menu({
		items:[{text:'载入盘点物料',handler:function(){
			afronction();
			
					}},{text:'批号设定'},{text:'保存',handler : function(){
						save();
					}},{text:'删除',handler : function(){
						
						del();
					}},{text:'修改',handler : function(){
						update();
					}}]
	});
var datajson;
//分页
var fielmenu2 =new Ext.menu.Menu({
	items : [ {
		text : '上一页',
		handler : function() {
			//alert(datajson.topPage+"..."+datajson.pagesize+"..."+datajson.currentPage)
			if(datajson.topPage>datajson.currentPage){
				alert("上一页没有信息了")
				return;
			}
			fenye(datajson.topPage);
			grids.getStore().removeAll();
		}
	}, {
		text : '下一页',
		handler : function() {
			//alert(datajson.nextPage)
			if(datajson.nextPage>datajson.pagesize-1){
				alert("下一页没有信息了")
				return;
			}
			fenye(datajson.nextPage);
			grids.getStore().removeAll();
		}
	} ]
	});

fenye(0);
function fenye(id){
	Ext.Ajax.request({
		// 被用来向服务器发起请求默认的url
		url : "http://localhost:8080/findZhuAll",
		// 请求时发送后台的参数,既可以是Json对象，也可以直接使用“name = value”形式的字符串
		params : {currentPage:id},
		// 请求时使用的默认的http方法
		method : "post",
		// 请求成功时回调函数
		success : function(data) {
			var temp = JSON.parse(data.responseText);
			datajson=temp;
			var temp1=datajson.pageobj[1].pandianxiang;
			Ext.getCmp('billNo').setValue(datajson.pageobj[0].billNo);
			Ext.getCmp('adjustDate').setValue(datajson.pageobj[0].adjustDate);
			Ext.getCmp('wareName').setValue(datajson.pageobj[0].wareName);
			Ext.getCmp('incSubjectName').setValue(datajson.pageobj[0].incSubjectName);
			Ext.getCmp('decSubjectName').setValue(datajson.pageobj[0].decSubjectName);
			Ext.getCmp('voucherNO').setValue(datajson.pageobj[0].voucherNO);
			Ext.getCmp('salesName').setValue(datajson.pageobj[0].salesName);
			Ext.getCmp('departName').setValue(datajson.pageobj[0].departName);
			Ext.getCmp('maker').setValue(datajson.pageobj[0].maker);
			Ext.getCmp('permitter').setValue(datajson.pageobj[0].permitter);
			grids.getStore().add(temp1);
		},
	});
}

var stroedt=Ext.create('Ext.data.Store', {
			 model: 'User',
			 fields:['name', 'dizhi','bianma', 'bianma','lianxi'],
			data:{'items':[
				{ 'name': '10111',  "dizhi":"北京",  "bianma":"40016","lianxi":"李忠"  },
				{ 'name': '10112',  "dizhi":"上海",  "bianma":"68016" ,"lianxi":"朱经理"},
				{ 'name': '10113', "dizhi":"天津",  "bianma":"408973" ,"lianxi":"周总" },
				{ 'name': '10114', "dizhi":"重庆", "bianma":"404567" ,"lianxi":"刘经理" }
			]},
				proxy: {
					type: 'memory',
					reader: {
						type: 'json',
						root: 'items'
					}
				}
 });



var grids=Ext.create('Ext.grid.Panel',{
    
	 listeners:{
				containerdblclick: function(grid, e, eOpts){ //单击事件
	    						grid.getStore().add({'name':'12', 'dizhi':'','bianma':'','lianxi':''});
    						
    					},
					itemcontextmenu:function(){
					
    						e.preventDefault();
    						new Ext.menu.Menu({
    							items:[{
    								xtype: 'textfield',
    								text: 'Lost',
    								pressed: false,
    								handler:function(){
											alert();
										}
    							},{
    								xtype: '',
    								text: '娓呴櫎鏂规',
    								pressed: false,
    								handler: function(){}
    							},{
    								xtype: '',
    								text: '閫夋嫨鏂规',
    								pressed: false,
    								menu:new Ext.menu.Menu({
    									items:[{
    										text:'棰勮鏂规',
    										pressed: false
    									}]
    								}),
    								handler: function(){}
    							},{
    								xtype: '',
    								text: '瀵煎嚭鑷矱xcel',
    								pressed: false,
    								handler: function(){}
    							},{
    								xtype: '',
    								text: '缃戞牸鎵撳嵃',
    								pressed: false,
    								handler: function(){}
    							}]
    						}
    						
    						).showAt(e.getXY());
				}
			},plugins: [
        				Ext.create('Ext.grid.plugin.CellEditing', {
            				clicksToEdit: 1
        				})
    				],
	columns: [
				 {header: '（栏号）',
	                    		xtype: 'rownumberer',
	                    		dataIndex: 'id', 
	                    		width: 50,
								sortable: false
        					},
					
        		{ header: '物料编号',  dataIndex: 'prodID', width: 100,editor : {
        			xtype : 'textfield',
        		}},
       			 { header: '物料名称', dataIndex: 'prodName',width: 120,editor : {
       				xtype : 'textfield',
       			} },
       				 { header: '规格型号', dataIndex: 'prodSize',editor : {
       					xtype : 'textfield',
       				} },
					 { header: '单位', dataIndex: 'stdUnitName',editor : {
							xtype : 'textfield',
						} },
					 { header: '账面数量', dataIndex: 'sumCurQty',editor : {
							xtype : 'textfield',
						}},
					 { header: '盘点数量',name: 'pandiansum',id: 'pandiansum', dataIndex: 'sumCheckQty',editor : {
							xtype : 'textfield',
						} },
					 { header: '盈亏数量', dataIndex: 'sumQty',editor : {
							xtype : 'textfield',
						} },
					 { header: '单价', dataIndex: 'suggestPrice',editor : {
							xtype : 'textfield',
						} },
					 { header: '盈亏金额', dataIndex: 'sumCost',editor : {
							xtype : 'textfield',
						} },
					 { header: '批号', dataIndex: 'pihao',editor : {
							xtype : 'textfield',
						} },
					 { header: '原因', dataIndex: 'yuanyin',editor : {
							xtype : 'textfield',
						} }
   					 ],
   			 height: 130,
			 width:1165,
			 autoScroll:false,
			 id:'grid1'
			 
})

var filterPanel = Ext.create('Ext.panel.Panel', {
    bodyPadding: 6,  // 避免Panel中的子元素紧邻边框
    width: 1180,
	height:170,
    items:[grids],
	bbar:[{xtype:"label",text:'总计:',width:90},
	{xtype:"textfield",disabled:true,width:90,margin:'0 180',value:'0.00'},
	{xtype:"textfield",disabled:true,width:90,margin:'0',value:'0.00'},
	{xtype:"textfield",disabled:true,width:90,margin:'0 40 0 30',value:'0.00'},
	{xtype:"textfield",disabled:true,width:90,margin:'0 0 0 0',value:'0.00'}],
});

var  tableds=Ext.create('Ext.TabPanel',{
	width:"100%",
	height:230,
	autoScroll:true,
	bodyPadding: 5,
	items:[{title:'内容',autoScroll:true,items:[filterPanel]},{title:'备注',xtype:'textarea',name:'Remark',fieldLabel: '备注',labelWidth: 30,}],
});


var formsg=Ext.create('Ext.form.Panel', {
    bodyPadding: 5,
	height:'100%',
	layout:"column",
	baseCls:'x-plain',
	items:[{
		 xtype: 'textfield',
        name: 'salesName',
        id: 'salesName',
		bodyPadding: 5,
        fieldLabel: '盘点人员',
		width:250,
        labelWidth: 80,
        anchor: '100%',
			},{
		 xtype: 'textfield',
        name: 'maker',
        id:'maker',
        fieldLabel: '制单人员',
		style:'margin-left:19px',
		width:250,
        labelWidth: 80,
        anchor: '100%',
			},{
		 xtype: 'textfield',
        name: 'departName',
        id: 'departName',
        fieldLabel: '所属部门',
		width:250,
        labelWidth: 80,
        anchor: '100%',
			},{
		 xtype: 'textfield',
        name: 'permitter',
        id: 'permitter',
        fieldLabel: '复核人员',
		style:'margin-left:19px',
		width:250,
        labelWidth: 80,
        anchor: '100%',
			}],
	});


var forms=Ext.create('Ext.form.Panel', {
    bodyPadding: 5,
	height:'100%',
	layout:"column",
	baseCls:'x-plain',
	items:[
	{
	             	fieldLabel: '盘点仓库',
					xtype:'combo',
	             	name: 'wareName',
	             	id: 'wareName',
	             	displayField: 'name',
					labelWidth:80,
					width:250,
    				valueField: 'abbr',
					store: Ext.create('Ext.data.Store', {
						fields: ['abbr', 'name'],
    					data : [
        					{"abbr":"原料仓库", "name":"原料仓库"},
        					{"abbr":"成品仓", "name":"成品仓"}
							
					   ]
					}),
					value: '',
        			typeAhead: true,
        			mode: 'local',
        			triggerAction: 'all',
        			selectOnFocus:true,
        			
	         	},{
		style:'margin-left:9px;color:blue',
		 xtype: 'datefield',
        name: 'adjustDate',
        id: 'adjustDate',
        fieldLabel: '单据日期',
		
		width:250,
        labelWidth: 70,
        anchor: '100%',
			},{
		 xtype: 'textfield',
        name: 'incSubjectName',
        id: 'incSubjectName',
        fieldLabel: '增值科目',
		width:250,
        labelWidth: 80,
        anchor: '100%',
			},{
			style:'margin-left:9px',
		 xtype: 'textfield',
        name: 'billNo',
        id: 'billNo',
		style:"color:blue;margin-left:9px",
        fieldLabel: '单据号码',
		width:250,
        labelWidth: 70,
        anchor: '100%',
			},{
		 xtype: 'textfield',
        name: 'decSubjectName',
        id: 'decSubjectName',
        fieldLabel: '减值科目',
		width:250,
        labelWidth: 80,
        anchor: '100%',
			},{
			style:'margin-left:9px',
		 xtype: 'textfield', //多行文本域
        name: 'voucherNO',
        id: 'voucherNO',
        fieldLabel: '凭证编号',
        labelWidth: 70,
		width:250,
        anchor: '100%',
			},{
			style:'margin-left:9px',
			 xtype: 'checkbox', //多行文本域
        	name: 'fapiaoshifou',
			hideLabels:true,
			checkboxToggle:true,
			
			boxLabel:"账面为零自动载入",
        	items:[{boxLabel:"账面为零自动载入",inputValue:1,checked:true}]
			},tableds,formsg
	]
    
	
	
});

//winform窗口
	var windowst=new  Ext.Window({
		width:640,
		height:570,
		title:"盘点单",
		closable:true,
		resizable:false, //设置是否可以改变大小
		draggable:true,
		anchor: '100%',
		items:[forms],
		/*listeners:{
			activate:function(){
				fenye(0);
			}
		},*/
		bbar:[{xtype:"splitbutton",text:'功能',width:90,menu:fielmenu},
		      {xtype:"splitbutton",text:'分页',width:90,menu:fielmenu2},
		      {xtype:"button",text:'审核',width:90,handler : function(){
		  		shenhe();
		  	}},
		      {xtype:"button",text:'反审核',width:90,handler : function(){
		  		fanshenhe();
		  	}}],
	});
	
	var windows2=new Ext.Window({
		width:400,
		height:300,
		title:"物料选择",
		closable:true,
		resizable:false, //设置是否可以改变大小
		draggable:true,
		closeAction:"hide",
	});
	
	function  afronction(){
		var song=Ext.encode(forms.getForm().getValues());
		Ext.Ajax.request(   
           	 {  
            //被用来向服务器发起请求默认的url  
           		 url : "",  
           	 //请求时发送后台的参数,既可以是Json对象，也可以直接使用“name = value”形式的字符串  
          		  params :[{}],  
            //请求时使用的默认的http方法  
            method : "post",  
            //请求成功时回调函数  
            success : function() {  
                Ext.ux.Toast.msg("信息提示", "成功删除所选记录！");  
            },  
            //请求失败时回调函数  
            failure : function() {  
                Ext.ux.Toast.msg("信息提示", "信息删除出错，请联系管理员!");  
            }  
        }  
    );  
				alert(song);// 序列化表单
	}
	
	
	//查询
	function findAll(){
		
	}
	
	function save(){
		alert(1);
		var grid=Ext.getCmp("grid1").getStore();
		var temp={
				billNo:Ext.getCmp('billNo').getValue(),
				adjustDate:Ext.getCmp('adjustDate').getValue(),
				wareName:Ext.getCmp('wareName').getValue(),
				incSubjectName:Ext.getCmp('incSubjectName').getValue(),
				decSubjectName:Ext.getCmp('decSubjectName').getValue(),
				voucherNO:Ext.getCmp('voucherNO').getValue(),
				salesName:Ext.getCmp('salesName').getValue(),
				departName:Ext.getCmp('departName').getValue(),
				maker:Ext.getCmp('maker').getValue(),
				permitter:Ext.getCmp('permitter').getValue(),
				//voucherNo:Ext.getCmp('voucherNo').getValue(),
				//currid:Ext.getCmp('currid').getValue(),
				pandianxiang:Ext.pluck(grid.data.items,"data"),
		}
	alert(JSON.stringify(temp));
		Ext.Ajax.request({
			url:'http://localhost:8080/InsertPanDianZhu',
			headers:{"Content-Type":"application/json"},
			params:JSON.stringify(temp),
			success:function(response){
				Ext.MessageBox.alert("提示","新增成功",function(){});
			}
		});
		
	}
	
	function del(){
		alert("删除。。。");
		var ss=Ext.getCmp('billNo').getValue();
		alert(ss)
		Ext.Ajax.request({
			// 被用来向服务器发起请求默认的url
			url : "http://localhost:8080/pandiandel",
			// 请求时发送后台的参数,既可以是Json对象，也可以直接使用“name = value”形式的字符串
			//headers:{"Content-Type":"application/json"},
			params :{BillNo:ss},
			// 请求时使用的默认的http方法
			method : "post",
			// 请求成功时回调函数
			success : function(data) {
				alert(JSON.stringify(data));
				Ext.MessageBox.alert("提示","删除成功",function(){}); 
			},
			//请求失败时回调函数  
            failure : function() {  
            	Ext.MessageBox.alert("提示","删除失败",function(){});  
            } 
		});
	}
	
	function update(){
		alert("修改");
		var grid=Ext.getCmp("grid1").getStore();
		var temp={
				billNo:Ext.getCmp('billNo').getValue(),
				adjustDate:Ext.getCmp('adjustDate').getValue(),
				wareName:Ext.getCmp('wareName').getValue(),
				incSubjectName:Ext.getCmp('incSubjectName').getValue(),
				decSubjectName:Ext.getCmp('decSubjectName').getValue(),
				voucherNO:Ext.getCmp('voucherNO').getValue(),
				salesName:Ext.getCmp('salesName').getValue(),
				departName:Ext.getCmp('departName').getValue(),
				maker:Ext.getCmp('maker').getValue(),
				permitter:Ext.getCmp('permitter').getValue(),
				pandianxiang:Ext.pluck(grid.data.items,"data"),
		}
	alert(JSON.stringify(temp));
		Ext.Ajax.request({
			url:'http://localhost:8080/pandianupdate',
			headers:{"Content-Type":"application/json"},
			params:JSON.stringify(temp),
			method : "post",
			success:function(response){
				Ext.MessageBox.alert("提示","修改成功",function(){});
			}
		});
	}
	
	function shenhe(){
		var grids = Ext.pluck(Ext.getCmp("grid1").getStore().data.items,"data");
		var billNo = Ext.getCmp('billNo').getValue();
		var data = {
				billNo:billNo,
				pandianxiang:grids
		}
		alert(JSON.stringify(data));
		
		
		Ext.Ajax.request({
			url:'http://localhost:8080/shenhexx',
			headers : {
				"Content-Type" : "application/json"
			},
			params:JSON.stringify(data),
			method : "post",
			success:function(response){
				Ext.MessageBox.alert("提示","审核通过",function(){});
			}
		});
	}
	
	function fanshenhe(){
		var grids = Ext.pluck(Ext.getCmp("grid1").getStore().data.items,"data");
		var billNo = Ext.getCmp('billNo').getValue();
		var data = {
				billNo:billNo,
				pandianxiang:grids
		}
		alert(JSON.stringify(data));
		
		
		Ext.Ajax.request({
			url:'http://localhost:8080/unshenhexx',
			headers : {
				"Content-Type" : "application/json"
			},
			params:JSON.stringify(data),
			method : "post",
			success:function(response){
				Ext.MessageBox.alert("提示","反审核通过",function(){});
			}
		});
	}